package com.wu.leetcode.leetcode23;

/**
 * Leetcode29-两数相除
 *
 * @author lynn
 * @date 2020/11/2 - 16:44
 */
public class Solution {
    public static int divide(int dividend, int divisor) {
        if (dividend == 0) {
            return 0;
        }

        if (divisor == 1) {
            return dividend;
        }
        if (divisor == -1 && dividend == Integer.MIN_VALUE) {
            return Integer.MAX_VALUE;
        }
        int sign = 1;
        if ((dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)) {
            sign = -1;
        }
        dividend = dividend < 0 ? dividend : -dividend;
        divisor = divisor < 0 ? divisor : -divisor;
        int res = div(dividend, divisor);
        return sign < 0 ? -res : res;
    }

    public static int div(int divi, int divor) {
        if (divi > divor) {
            return 0;
        }
        int val = divor;
        int count = 1;
        while (val + val < 0 && divi <= (val + val)) {
            val <<= 1;
            count <<= 1;
        }

        return count + div(divi - val, divor);
    }

    public static void main(String[] args) {
        int div = divide(-2147483648, 2);
        System.out.println(div);
    }
}
